/*This trigger updates the profile assigned to the user before
 *inserting (eg: when a user submits a registration form) and
 *before updating (eg: after user edits profile).
 */

trigger updateProfile on User (before update) {
    User user = trigger.new[0];
    Person__c person = [SELECT Name, Is_Tutor__c FROM Person__c WHERE Name =: user.UTORid__c];
    if (user.isTutor__c && user.ProfileId == '00eU0000000MV2nIAG') {
        /* Assign Tutor Profile */
        user.ProfileId = '00eU0000000MWbzIAG';
        person.Is_Tutor__c = true;
        update person;
    }
    else if (!user.isTutor__c && user.ProfileId == '00eU0000000MWbzIAG') {
        /* Assign Student Profile
         * TO-DO: making sure admin profile does not get overwritten
         */
        user.ProfileId = '00eU0000000MV2nIAG';
        person.Is_Tutor__c = false;
        update person;
    }
}